﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;

/**
 * Copyright 2011 by Alberto Gemin
 * agemin@hotmail.com
 * Version 1.2
 * 27 Aug 2011
 **/
namespace Gemina.CRM2011.WF.CRM2011ShareStep
{

    /// <summary>
    /// Custom base class for all Code Activities.
    /// </summary>
    public abstract class CodeActivityBase : CodeActivity
    {

        #region Properties

        /// <summary>
        /// Primary Key of the entity the WF runs on.
        /// </summary>
        public Guid CurrentRecordId { get; set; }

        /// <summary>
        /// Name of the entity the WF runs on.
        /// </summary>
        public string CurrentEntityName { get; set; }

        /// <summary>
        /// Operation that triggered the workflow.
        /// </summary>
        public string CurrentOperation { get; set; }

        /// <summary>
        /// CRM Web Service.
        /// </summary>
        public IOrganizationService OrganizationService;

        /// <summary>
        /// CRM Web Service factory.
        /// </summary>
        public IOrganizationServiceFactory OrganizationServiceFactory;

        /// <summary>
        /// Tracing service.
        /// </summary>
        public ITracingService TracingService;

        /// <summary>
        /// Workflow context.
        /// </summary>
        public IWorkflowContext WorkflowContext;

        #endregion

        #region CodeActivity

        /// <summary>
        /// Workflow step.
        /// </summary>
        protected override void Execute(CodeActivityContext executionContext)
        {
            this.Trace("CodeActivityBase.Execute: Start");

            try
            {
                this.WorkflowContext = executionContext.GetExtension<IWorkflowContext>();
                this.OrganizationServiceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                this.OrganizationService = this.OrganizationServiceFactory.CreateOrganizationService(this.WorkflowContext.UserId);
                this.CurrentRecordId = this.WorkflowContext.PrimaryEntityId;
                this.CurrentOperation = this.WorkflowContext.MessageName;
                this.CurrentEntityName = this.WorkflowContext.PrimaryEntityName;
                this.ExecuteBody(executionContext);
            }
            catch (AgErrorMessage ex)
            {
                this.Trace(ex.ToString());
                throw new InvalidPluginExecutionException(OperationStatus.Failed, ex.Message);
            }
            catch (Exception ex)
            {
                this.Trace(ex.ToString());
                throw new InvalidPluginExecutionException(OperationStatus.Failed, ex.ToString());
            }

            this.Trace("CodeActivityBase.Execute: End");
        }

        /// <summary>
        /// Overridden in derived classes: contains the actual workflow step.
        /// </summary>
        protected abstract bool ExecuteBody(CodeActivityContext executionContext);

        #endregion

        #region Helpers

        /// <summary>
        /// Trace and handle creation of tracing service.
        /// </summary>
        protected void Trace(string msg)
        {/*
            if (this.TracingService == null)
            {
                if (this.ExecutionContext == null)
                {
                    return;
                }
                else
                {
                    this.TracingService = this.ExecutionContext.GetExtension<ITracingService>();
                }
            }
            this.TracingService.Trace(msg);
        */
        }

        #endregion

    }

}
